#coding:utf-8
#!/usr/bin/env python

import sqlite3

conn = sqlite3.connect("add.db")
conn.isolation_level = None
conn.execute("create table if not exists myadd(id integer primary key autoincrement,name vachar(128),email varchar(128))")
cur = conn.cursor()

def showadd():
    #cur = conn.cursor()
    cur.execute("select * from myadd")
    res = cur.fetchall()
    print '-'*60
    for line in res:
        for f in line:
            print str(f).ljust(15),
        print 
    print '-'*60

def appnew():
    myname = raw_input("Input your name:")
    myemail = raw_input("Input your email:")
    cur.execute("insert into myadd(name,email) values('%s', '%s')"%(myname, myemail))
    conn.commit()

def deleteadd():
    delname = raw_input("Input name:")
    cur.execute("delete from myadd where name = '%s'"%delname)
    conn.commit()

def showone():
    showname = raw_input("Input name:")
    cur.execute("select * from myadd where name='%s'"%showname)
    res = cur.fetchone()
    if res != None:
        print '-'*60
        for line in res:
            print str(line).ljust(15),
        print
        print '_'*60
    else:
        print 'Data not found!'

def showmenu():

    a=" "
    print a.ljust(20),'='*30
    print a.ljust(26),'(1)New data'
    print a.ljust(26),'(2)Delete date'
    print a.ljust(26),'(3)Show one'
    print a.ljust(26),'(4)Show All'
    print a.ljust(26),'(Q)Quit'
    print a.ljust(20),'='*30

if __name__ == '__main__':
    while True:
        showmenu()
        sel = raw_input("Your choose:")
        if sel == '4':
            showadd()
        elif sel == '2':
            deleteadd()
        elif sel == '3':
            showone()
        elif sel == '1':
              appnew()
        elif sel == 'Q' or sel =='q':
            break
        else:
            print 'Error input!'
    
    cur.close()
    conn.close()
